/**
 * related to selectArea.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-22
 */
var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var areaList = ui("areaList");
var listdata = mm("do_ListData");
areaList.bindItems(listdata);

var common = require("common");
var areaUrl = require("url/sys");
var qqHttp = require("helper/http");

//andriod返回键
page.on("back", function() {
    app.closePage();
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "我的地址"
})
header.refreshData();

var param = {"provinceId":"","provinceName":"","cityId":"","cityName":"","regionId":"","regionName":"","parentId":"","level":""};
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var parentId = "";
	//读取当前页面的传入参数
	var data = page.getData();
	if (data) {
		var parentId = data.parentId;
		param.provinceId = data.provinceId;
		param.provinceName = data.provinceName;
		param.cityId = data.cityId;
		param.cityName = data.cityName;
		param.regionId = data.regionId;
		param.regionName = data.regionName;
	}
	qqHttp.post(areaUrl.url.area.getAreaByParent, {"parentId":parentId}, function(data) {
		deviceone.print(JSON.stringify(data));
		listdata.addData(data.list);
		areaList.refreshItems();
	});
});

//点击一条
areaList.on("touch", function(data){
	var one = listdata.getOne(data);
	deviceone.print(JSON.stringify(one));
	if (one.level == 1) {
		param.provinceId = one.id;
		param.provinceName = one.nameAll;
	} else if (one.level == 2) {
		param.cityId = one.id;
		param.cityName = one.nameAll;
	} else if (one.level == 3) {
		param.regionId = one.id;
		param.regionName = one.nameAll;
	}
	param.parentId = one.id;
	param.level = one.level;
	if (one.level != 3) {
		common.open("source://view/area/selectArea.ui",param);
	} else {
		app.closePage(param,null,3);
	}
});
